package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SalaryList;
import cn.wolfcode.crm.query.SalaryListQueryObject;
import cn.wolfcode.crm.service.ISalaryListService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("salaryList")
public class SalaryListController {
    @Autowired
    public ISalaryListService salaryListService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"salaryList:view", "工资表列表"}, logical = Logical.OR)
    public String view() {
        return "salaryList/salaryList";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(SalaryListQueryObject qo) {
        return salaryListService.query(qo);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<SalaryList> selectAll() {
        return salaryListService.selectAll();
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"salaryList:delete", "工资表删除"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            salaryListService.deleteByPrimaryKey(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("删除失败");
        }
    }
    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"salaryList:saveOrUpdate", "工资表新增或编辑"}, logical = Logical.OR)
    public JSONResult saveOrUpdate(SalaryList sal) {
        try {
            if (sal.getId() != null) {
                salaryListService.updateByPrimaryKey(sal);
            } else {
                salaryListService.insert(sal);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value = {"salaryList:changeState", "工资表离职或复职"}, logical = Logical.OR)
    public JSONResult changeState(Long id) {
        try {
            salaryListService.changState(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
}

